package com.nlp.word.link;

public class WordLink {
	
	public Node root;
	
	public void add(String key) {
		if (root == null) {//如果根节点不存在则创建
			root = new Node(key.charAt(0));
		}
		Node parNode = root;//父节点
		for(int i = 1; i < key.length(); i ++) {//从前往后逐个放入字符
			char c = key.charAt(i);
			Node currNode = new Node(c);//当前节点
			currNode.setFreq(1);
			parNode.setFreq(0);
			parNode.next = currNode;//当前节点作为父节点的孩子节点
			parNode = currNode;
			
		}
	}
	
	public boolean find(String input) {
		Node curNode = root;//当前节点
		int i = 0;
		while (curNode != null && i < input.length()) {
			if (curNode.element != input.charAt(i)) {
				return false;
			}else if (curNode.freq > 0) {
				return true;
			}
			//节点和字符位置同步前进一步
			curNode = curNode.next;
			i++;
		}
		return false;//没有找到
	}
	
	public static void main(String[] args) {
		String key = "abcdef";
		WordLink link = new WordLink();
		link.add(key);
		String input = "abcd";
		boolean find = link.find(input);
		System.out.println(find);
	}

}
